--[[--
类名: OptionButtonMenu.lua
创建日期: 2015-12-16
创建人: 金剑
功能描述: 选项按钮集合
修改内容：
修改日期:   
修改人:     
修改备注: 
]]

local Vector = require("app.utils.Vector")

local OptionButtonMenu = class("OptionButtonMenu")

function OptionButtonMenu:ctor()
    self.mOptionVec = Vector.new()

    --按钮生效事件类型,默认 触摸结束时
    self.touchEffectiveType = ccui.TouchEventType.ended
end

function OptionButtonMenu:getSize()
    return self.mOptionVec:size()
end

function OptionButtonMenu:addCheckBox(checkBox)
    self.mOptionVec:add(checkBox)
    local count = self.mOptionVec:size()
    self:resetListener(count)

    if count == 1 then
        self:switchOption(count)
    end

end

function OptionButtonMenu:addCheckBoxNotSelected(checkBox)
    self.mOptionVec:add(checkBox)
    local count = self.mOptionVec:size()
    self:resetListener(count)
end

function OptionButtonMenu:getCheckBoxByIndex(index)
    return self.mOptionVec:get(index)
end

function OptionButtonMenu:getCheckBoxIndexByTag(tag)
    for i = 1, self.mOptionVec:size() do
        local btn = self.mOptionVec[i]
        if btn:getTag() == tag then
            return i
        end
    end
    return nil
end

function OptionButtonMenu:setLongPressCallback(callback,longPressTime)
    self.mLongPressCallback = callback
    self.mLongPressTime = longPressTime or 1.0
end

function OptionButtonMenu:setButtonCallback(callback)
    self.mButtonTouchCallBack = callback
end

function OptionButtonMenu:setCallback(callback)
    self.mCallback = callback
end

function OptionButtonMenu:setUnSelectCallback(callback)
    self.mUnSelectCallback = callback
end

function OptionButtonMenu:setCallback1(callback1)
    self.mCallback1 = callback1
end

--function OptionButtonMenu:select(index)
--    local btn = self.mOptionVec[index]
--    btn:setSelected(true)
--    self:resetListener()
--end

function OptionButtonMenu:resetListener(index)
    local function selectedStateEvent(sender,eventType)
        if self.mButtonTouchCallBack then
            self.mButtonTouchCallBack(sender,eventType)
        end

        local function touchEffectiveCheck()
            if self.touchEffectiveType == eventType then
                local isOldSelect = sender.isSelect
                if not isOldSelect then
                    print("----------------selected index:"..index)
                    self:switchOption(index)
                else
                    print("-------------------unselected index:"..index)
                    -- sender:setSelected(true)
                    local isRealSelect = true
                    
                    if self.mCallback ~= nil then
                        isRealSelect = self.mCallback(sender, false)
                    end

                    if isRealSelect then
                        sender.isSelect = true
                    end
                end
            end
        end

        if eventType == ccui.TouchEventType.began then
            if self.mLongPressCallback then
                --如果有长按回调
                --记录触摸时间
                sender.touchTime = app:getTime()
                local actions = {
                    cc.DelayTime:create(self.mLongPressTime),
                    cc.CallFunc:create(function() 
                        if self.mLongPressCallback then
                            self.mLongPressCallback(sender, ccui.TouchEventType.began)
                        end
                    end)
                }
                local action = cc.Sequence:create(actions)
                action:setTag(Enums.CommonActionTag.LongPress)
                sender:runAction(action) 
            end
            
            touchEffectiveCheck()
        elseif eventType == ccui.TouchEventType.ended then
            local isGoOn = true
            if self.mLongPressCallback then
                local durTime = app:getTime() - sender.touchTime
                if durTime >= self.mLongPressTime then
                    --长按
                    isGoOn = false
                else
                    --短按
                    sender:stopActionByTag(Enums.CommonActionTag.LongPress)
                end

                if self.mLongPressCallback then
                    self.mLongPressCallback(sender, ccui.TouchEventType.ended)
                end
            end

            if isGoOn then
                -- local isOldSelect = sender.isSelect
                -- if not isOldSelect then
                --     print("----------------selected index:"..index)
                --     self:switchOption(index)
                -- else
                --     print("-------------------unselected index:"..index)
                --     -- sender:setSelected(true)
                --     local isRealSelect = true
                    
                --     if self.mCallback ~= nil then
                --         isRealSelect = self.mCallback(sender, false)
                --     end

                --     if isRealSelect then
                --         sender.isSelect = true
                --     end
                -- end

                touchEffectiveCheck()
            end
        elseif eventType == ccui.TouchEventType.canceled then
            if self.mLongPressCallback then
                self.mLongPressCallback(sender, ccui.TouchEventType.canceled)
            end
        end
    end

    local btn = self.mOptionVec[index]
    btn:addTouchEventListener(selectedStateEvent)
    -- btn:setSelected(false)
    btn.isSelect = false

    if self.mUnSelectCallback then
        self.mUnSelectCallback(btn:getTag())
    end
end

function OptionButtonMenu:switchOption(index, isNotCallback)
    local function switchOption(button)
        if not button:isEnabled() then
            --如果按钮设置了disenabled，但是那么点击也应该是不选中
            -- button:setSelected(false)
            return
        end

        local selectBtn = button

        local isRealSelect = true
        if selectBtn then
            -- selectBtn:setSelected(true)
            if not isNotCallback then
                if self.mCallback ~= nil then
                    isRealSelect = self.mCallback(selectBtn, true)
                end
                if self.mCallback1 then
                    self.mCallback1(selectBtn:getName())
                end
            end

            --如果有回调，从回调中判断是否真的可以选中
            if isRealSelect then
                selectBtn.isSelect = true
            end

        end

        if isRealSelect then
            for i = 1, self.mOptionVec:size() do
                local btn = self.mOptionVec[i]

                if btn ~= button then
                    -- btn:setSelected(false)
                    btn.isSelect = false
                    if self.mUnSelectCallback then
                        self.mUnSelectCallback(btn:getTag())
                    end
                end
            end
        end
    end

    local selIndex = 1
    if index then
        selIndex = index
    end

    local count = self.mOptionVec:size()
    if selIndex <= count then
        local btn = self.mOptionVec[selIndex]
        switchOption(btn)
    end
end

function OptionButtonMenu:unselectAllOption()
    for i = 1, self.mOptionVec:size() do
        local btn = self.mOptionVec[i]
        -- btn:setSelected(false)
        btn.isSelect = false
        if self.mUnSelectCallback then
            self.mUnSelectCallback(btn:getTag())
        end
    end
end

function OptionButtonMenu:setTouchEffectiveType(type)
    self.touchEffectiveType = type
end

return OptionButtonMenu